﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLib;

namespace Cinema
{
    public class FilmScreening : IFilmScreening
    {
        private IFilm _film;
        private TimeRange _screeningTime;
        private ushort _ticketsSold;

        public FilmScreening(IFilm film, TimeRange screeeningTime) : this(film, screeeningTime, 0)
        {
        }

        public FilmScreening(IFilm film, TimeRange screeeningTime, ushort ticketsSold)
        {
            //
            // initialize the film first!!! Other properties will use it for validation!!!
            Film = film;
            ScreeningTime = screeeningTime;
            TicketsSold = ticketsSold;
        }

        public IFilm Film
        {
            get { return _film; }
            protected set
            {
                CheckFilm(value);
                _film = value;
            }
        }

        public TimeRange ScreeningTime
        {
            get
            {
                return _screeningTime;
            }
            set
            {
                if (value.StartTime.AddMinutes(Film.Duration.Value) > value.EndTime)
                    throw new ArgumentException(
                        "The screening time range is less than the film duration", "endtime");

                _screeningTime = value;
            }
        }

        public ushort TicketsSold
        {
            get
            {
                return _ticketsSold;
            }
            set
            {
                _ticketsSold = value;
            }
        }

        public FilmScreeningUniqueKey UniqueKey
        {
            get
            {
                return new FilmScreeningUniqueKey(Film.UniqueKey, ScreeningTime.StartTime);
            }
        }

        public static void CheckFilm(IFilm film)
        {
            if (film == null)
                throw new ArgumentException("The film can't be null", "film");
        }

    }
}
